package com.sip.stream.utils.select;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.sip.stream.R;
import com.sip.stream.adapter.SelectItemDialogAdapter;

import java.util.ArrayList;
import java.util.List;

public class SelectDialogUtils {

    private static SelectItemDialogAdapter selectItemDialogAdapter;

    /**
     * 选择弹窗列表
     * @param context
     * @param listener
     */
    public static void showSelectListDialog(Context context, List<String> data, OnSelectDialogListener listener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogCustom);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.select_config_dialog_layout, null);
        RecyclerView itemView = dialogView.findViewById(R.id.itemView);
        itemView.setLayoutManager(new LinearLayoutManager(context));
        builder.setView(dialogView);
        AlertDialog alertDialog = builder.create();
        selectItemDialogAdapter = new SelectItemDialogAdapter(context, new OnSelectDialogListener() {
            @Override
            public void onSelectItemBean(String item) {
                ToastUtils.showShort("选择了： "+ item);
                listener.onSelectItemBean(item);
                alertDialog.dismiss();
            }
        });
        itemView.setAdapter(selectItemDialogAdapter);
        selectItemDialogAdapter.setData(data);
        alertDialog.show();
    }


    /***
     * 获取相机分辨率
     * @param context
     * @return
     */
    public static String[] getSupportSizeCamera2(Context context) {
        CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        try {
            String camera2Id = getCamera2Id(false, false, false, cameraManager);
            if (camera2Id == null) {
                camera2Id = cameraManager.getCameraIdList()[0];
            }
            Size[] outputSizes = ((StreamConfigurationMap) cameraManager.getCameraCharacteristics(camera2Id).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)).getOutputSizes(35);
            ArrayList arrayList = new ArrayList();
            for (Size size : outputSizes) {
                if (size.getHeight() >= 360 && ((int) ((size.getHeight() / 9.0f) * 16.0f)) == size.getWidth()) {
                    arrayList.add("" + size.getWidth() + "X" + size.getHeight());
                }
            }
            return (String[]) arrayList.toArray(new String[arrayList.size()]);
        } catch (CameraAccessException e) {
            e.printStackTrace();
            return new String[0];
        }
    }

    public static String getCamera2Id(boolean frontSide, boolean isInfraredLightOpen, boolean isQRScan, CameraManager cameraManager) {
        try {
            String[] cameraIdList = cameraManager.getCameraIdList();
            if (cameraIdList.length == 4) {
                if (frontSide) {
                    return cameraIdList[1];
                }
                if (isInfraredLightOpen) {
                    return cameraIdList[3];
                }
                if (isQRScan) {
                    return cameraIdList[0];
                }
                return cameraIdList[2];
            }
            return null;
        } catch (CameraAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
